package com.bw.z.mvp.view.activity;


import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.bw.z.R;
import com.bw.z.mvp.view.live.LiveFragment;
import com.bw.z.mvp.view.news.NewsFragment;
import com.bw.z.mvp.view.people.PeopleFragment;
import com.bw.z.mvp.view.user.UserFragment;
import com.bw.z.mvp.view.video.VideoFragment;
import com.bw.z.mvp.base.view.BaseActivity;
import com.bw.z.mvp.contract.SplashContract;
import com.bw.z.mvp.di.DaggerSplashComponent;
import com.bw.z.mvp.di.SplashModules;
import com.bw.z.mvp.presenter.SplashPresenter;
import com.bw.z.utils.ToastUtils;
import com.bw.z.widget.BottomButton;

public class MainActivity extends BaseActivity<SplashPresenter>
        implements BottomButton.SelectListener, SplashContract.ISplashView {

    private BottomButton bottomNews;
    private BottomButton bottomPeople;
    private BottomButton bottomVideo;
    private BottomButton bottomLive;
    private BottomButton bottomUser;

    NewsFragment newsFragment;
    PeopleFragment peopleFragment;
    VideoFragment videoFragment;
    LiveFragment liveFragment;
    UserFragment userFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_button);
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            }, 101);
        }
        bottomNews.setContent("新闻").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu1_f)
                .setSelImg(R.mipmap.tu1_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottomPeople.setContent("人民号").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu2_f)
                .setSelImg(R.mipmap.tu2_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottomVideo.setContent("视频").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu3_f)
                .setSelImg(R.mipmap.tu3_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottomLive.setContent("直播").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu4_f)
                .setSelImg(R.mipmap.tu4_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        bottomUser.setContent("我的").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.mipmap.tu5_f)
                .setSelImg(R.mipmap.tu5_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        newsFragment = new NewsFragment();
        fragmentTransaction.add(R.id.bottom_ll, newsFragment);
        fragmentTransaction.commit();

//        UpdateConfig config = new UpdateConfig(true, true, false, false, false
//                , "", "apk.1.1"
//                , DownLoadBy.APP
//                , false, true
//                , 0
//                , false, true,
//                "", 2);
//        UpdateAppUtils.getInstance().apkUrl("upDataEntity.getUrl()")
//                .updateTitle("更新版本")
//                .updateContent("更新新版本？要不然滚犊子")
//                .updateConfig(config)
//                .update();

    }

    @Override
    public View addStatusView() {
        return f(R.id.main_linear);
    }

    @Override
    public void onSelect(int id) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (newsFragment != null) {
            fragmentTransaction.hide(newsFragment);
        }
        if (peopleFragment != null) {
            fragmentTransaction.hide(peopleFragment);
        }
        if (videoFragment != null) {
            fragmentTransaction.hide(videoFragment);
        }
        if (liveFragment != null) {
            fragmentTransaction.hide(liveFragment);
        }
        if (userFragment != null) {
            fragmentTransaction.hide(userFragment);
        }

        switch (id) {
            case R.id.bottom_news:
                bottomPeople.clearSelect();
                bottomVideo.clearSelect();
                bottomLive.clearSelect();
                bottomUser.clearSelect();
                fragmentTransaction.show(newsFragment);
                getColors(Color.WHITE);
                break;
            case R.id.bottom_people:
                bottomNews.clearSelect();
                bottomVideo.clearSelect();
                bottomLive.clearSelect();
                bottomUser.clearSelect();
                if (peopleFragment == null) {
                    peopleFragment = new PeopleFragment();
                    fragmentTransaction.add(R.id.bottom_ll, peopleFragment);
                } else {
                    fragmentTransaction.show(peopleFragment);
                }
                getColors(Color.WHITE);
                break;
            case R.id.bottom_video:
                bottomPeople.clearSelect();
                bottomNews.clearSelect();
                bottomLive.clearSelect();
                bottomUser.clearSelect();
                if (videoFragment == null) {
                    videoFragment = new VideoFragment();
                    fragmentTransaction.add(R.id.bottom_ll, videoFragment);
                } else {
                    fragmentTransaction.show(videoFragment);
                }
                getColors(Color.WHITE);
                break;
            case R.id.bottom_live:
                bottomPeople.clearSelect();
                bottomVideo.clearSelect();
                bottomNews.clearSelect();
                bottomUser.clearSelect();
                if (liveFragment == null) {
                    liveFragment = new LiveFragment();
                    fragmentTransaction.add(R.id.bottom_ll, liveFragment);
                } else {
                    fragmentTransaction.show(liveFragment);
                }
                getColors(Color.WHITE);
                break;
            case R.id.bottom_user:
                bottomPeople.clearSelect();
                bottomVideo.clearSelect();
                bottomLive.clearSelect();
                bottomNews.clearSelect();
                if (userFragment == null) {
                    userFragment = new UserFragment();
                    fragmentTransaction.add(R.id.bottom_ll, userFragment);
                } else {
                    fragmentTransaction.show(userFragment);
                }
                getColors(Color.parseColor("#eb5555"));
                break;
        }
        fragmentTransaction.commit();
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_bottom_button;
    }

    public void initView() {
        getColors(Color.WHITE);
        bottomNews = findViewById(R.id.bottom_news);
        bottomPeople = findViewById(R.id.bottom_people);
        bottomVideo = findViewById(R.id.bottom_video);
        bottomLive = findViewById(R.id.bottom_live);
        bottomUser = findViewById(R.id.bottom_user);
    }

    @Override
    public void inject() {
        DaggerSplashComponent.builder().splashModules(new SplashModules(this)).build().injectBB(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void refresh() {

    }


//    //权限方法
//    private void requestPermission() {
//        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
//            return;
//        if (ContextCompat.checkSelfPermission(
//                this, Manifest.permission.READ_EXTERNAL_STORAGE) ==
//                PackageManager.PERMISSION_GRANTED &&
//                ContextCompat.checkSelfPermission(
//                        this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
//                        PackageManager.PERMISSION_GRANTED &&
//                ContextCompat.checkSelfPermission(
//                        this, Manifest.permission.CAMERA) ==
//                        PackageManager.PERMISSION_GRANTED) {
//            requestPermissions(new String[]{
//                    Manifest.permission.READ_EXTERNAL_STORAGE,
//                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                    Manifest.permission.CAMERA,
//            }, REQUESTPERMISSIONCODE);
//        }
//    }

    //获取申请结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean flag = true;
        if (ContextCompat.checkSelfPermission(
                this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED)
            flag = false;

        if (ContextCompat.checkSelfPermission(
                this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED)
            flag = false;

        if (ContextCompat.checkSelfPermission(
                this, Manifest.permission.CAMERA) !=
                PackageManager.PERMISSION_GRANTED)
            flag = false;

        if (!flag) {
            ToastUtils.showMsg("请手动赋予当前应用程序权限");
            finish();
        }
    }
}